//
//  UIImage+GX.m
//  tools
//
//  Created by 曹曹 on 16/8/20.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "UIImage+GX.h"

@implementation UIImage (GX)

#pragma mark - 获得给定颜色和大小的图片
/**
 *  获得给定颜色和大小的图片
 *
 *  @param color 指定的图片颜色
 *  @param size  指定返回的图片大小
 *
 *  @return 返回指定颜色和大小的图片
 */
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size {
    CGFloat imageW = size.width;
    CGFloat imageH = size.height;
    // 1.开启基于位图的图形上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(imageW, imageH), NO, 0.0f);
    // 2.画一个color颜色的矩形框
    [color set];
    UIRectFill(CGRectMake(0, 0, imageW, imageH));
    // 3.拿到图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.关闭上下文
    UIGraphicsEndImageContext();
    
    return image;
}

#pragma mark - 圆形图片
/**
 圆形图片
 
 @param name        待处理的图片
 @param borderWidth 边框宽度
 @param borderColor 边框颜色
 
 @return 处理完的圆形图片
 */
+ (UIImage *)circleImageWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    // 1.加载图片
    UIImage *originImage = [UIImage imageNamed:name];
    UIImage *newImage = [originImage circleImageWithBorderWidth:borderWidth borderColor:borderColor];
    return newImage;
}

- (UIImage *)circleImageWithBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    CGFloat imageW = self.size.width + 2 * borderWidth;
    CGFloat imageH = imageW;
    CGSize imageSize = CGSizeMake(imageW, imageH);
    // 2.开启基于位图的图形上下文
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    // 3.取得当前上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置当前图形上下文的填充(fill)和描边(stroke)颜色为borderColor
    [borderColor set];
    // 4.画边框(大圆)
    CGFloat bigRadius = imageW * 0.5;//大圆半径
    CGFloat centerX = bigRadius;//圆心
    CGFloat centerY = bigRadius;
    /**
     给上下文的通道添加一个圆弧

     @param c          上下文
     @param x          中心点的x
     @param y          中心点的y
     @param radius     半径
     @param startAngle 开始的角度
     @param endAngle   结束的角度
     @param clockwise  顺时针1 逆时针0

     @return 无
     */
    //CGContextAddArc(CGContextRef cg_nullable c, CGFloat x, CGFloat y,CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
    CGContextAddArc(context, centerX, centerY, bigRadius, 0, M_PI * 2, 0);
    // 用填充色填充圆弧内的空间
    CGContextFillPath(context);//得到一个圆形的背景图片(填充色)
    // 5.小圆
    CGFloat smallRadius = self.size.width / 2;
    // 在圆形背景图片上画圆弧
    CGContextAddArc(context, centerX, centerY, smallRadius, 0, M_PI * 2, 0);
    // 将背景图片上圆弧内的部分剪裁掉(此时得到一个宽度为borderWidth的圆环)
    CGContextClip(context);
    // 6.将图片(self)放入矩形范围, 图片会以圆形显示
    [self drawInRect:CGRectMake(borderWidth, borderWidth, self.size.width, self.size.height)];
    // 7.取图
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 8.结束上下文
    UIGraphicsEndImageContext();
    return newImage;
}

#pragma mark - 使用blend改变图片的颜色
/**
 使用blend改变图片的颜色

 @param tintColor 目标颜色
 @param blendMode 融合模式

 @return 融合后的图片
 */
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode {
    // We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    // Fill color
    [tintColor setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    // Fill the context
    UIRectFill(bounds);
    // Draw the tinted image in context
    // kCGBlendModeDestinationIn 虽然保留了原色的透明度，但是却把它的所有的灰度信息弄丢了
    // kCGBlendModeOverlay 可以保持背景色的明暗，也就是灰度信息,但是透明度又没了
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];
    if (blendMode != kCGBlendModeDestinationIn) {
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    }
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tintedImage;
}

#pragma mark - 获取view所对应的图片
/**
 *  获取view所对应的图片
 *
 *  @param view 目标view
 *
 *  @return image
 */
+ (instancetype)captureImageWithViwe:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - 生成一张带有图标的图片
/**
 生成一张带有图标的图片

 @param image 背景图片
 @param icon  图片图标

 @return 带有图标的图片
 */
+ (UIImage *)createNewImageWithImage:(UIImage *)image icon:(UIImage *)icon {
    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    CGFloat iconW = image.size.width / 6;
    CGFloat iconH = iconW * icon.size.height / icon.size.width;
    CGFloat iconX = (image.size.width - iconW);
    CGFloat iconY = (image.size.height - iconH);
    [icon drawInRect:CGRectMake(iconX, iconY, iconW, iconH)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
